import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import player.PlayerFrame;

/**
 * Created by Dominik on 2014-10-18.
 * Cahnged by Agnieszka on 2014-10-19.
 */
public class Player implements Runnable{
	
    private static String CHECK = "Check";
    private static String RAISE = "Raise";
    private static String CALL = "Call";
    private static String FOLD = "Fold";
    private static String ALL_INN = "All-in";
    private static String DRAW = "Draw";
    private static String SEND = "Send";
    
	private String name;
    private int number;
    private int coins;
    private Hand PlayerHand;
    private SocketServer server;
    private Socket client;
    boolean works;
    BufferedReader in = null;
	PrintWriter out = null;

    

    public Player(String name, int number,Socket client, SocketServer server, int startingCoins, Deck deck)
    {
        this.name = name;
        this.number = number;
        this.coins = startingCoins;
        this.PlayerHand = new Hand(deck, this);
        this.client = client;
		this.server = server;
		this.works = true;
        
        System.out.println("tutaj!");
    }
    
    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }


    public int getCoins() {
        return coins;
    }

    public Hand getPlayerHand() {
        return PlayerHand;
    }

    public void setCoins(int coins) {
        this.coins = coins;
    }
    public void addNewPlayer(String name){
    	out.println("addNewPlayer" + name);
    }
    
	public void run()
	{
		String line = "";
		int dane;
		try
		{
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			out = new PrintWriter(client.getOutputStream(), true);
		} 
		catch (IOException e) 
		{
			System.out.println("in or out failed");
			System.exit(-1);
		}
		while(line !=null && works)
		{
			try
			{
				if (line.matches("CH")) 
				{
					line = in.readLine();
					dane = Integer.parseInt(line.substring(2));
					out.println("O " + dane);
				}
				else if (line.matches("Q")) 
				{
					System.out.println( "Client" + client.getInetAddress() + " quited.");
					client.close();
					this.works = false;
				}
				else
				{
					out.println("E " + "Illegal arguments." );
				}
			}
			catch (NumberFormatException e )
			{
				out.println("E Illegal argument." );
			}
			catch (IOException e)
			{
				System.out.println("E Read failed.");
			}
		}
		
	}
}
